home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Video / getbits.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-02  |  5.4 KB  |  230 lines

  1. /* getbits.c, bit level routines                                            */
  2.  
  3. /*
  4.  * All modifications (mpeg2decode -> mpeg2play) are
  5.  * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
  6.  */
  7.  
  8. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  9.  
  10. /*
  11.  * Disclaimer of Warranty
  12.  *
  13.  * These software programs are available to the user without any license fee or
  14.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  15.  * any and all warranties, whether express, implied, or statuary, including any
  16.  * implied warranties or merchantability or of fitness for a particular
  17.  * purpose.  In no event shall the copyright-holder be liable for any
  18.  * incidental, punitive, or consequential damages of any kind whatsoever
  19.  * arising from the use of these programs.
  20.  *
  21.  * This disclaimer of warranty extends to the user of these programs and user's
  22.  * customers, employees, agents, transferees, successors, and assigns.
  23.  *
  24.  * The MPEG Software Simulation Group does not represent or warrant that the
  25.  * programs furnished hereunder are free of infringement of any third-party
  26.  * patents.
  27.  *
  28.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  29.  * are subject to royalty fees to patent holders.  Many of these patents are
  30.  * general enough such that they are unavoidable regardless of implementation
  31.  * design.
  32.  *
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #include "config.h"
  39. extern "C"
  40. {
  41. #include "global.h"
  42. }
  43.  
  44. #include "..\Video\VideoWrapper.h"
  45. #include "..\Subpic\Subpic.h"
  46.  
  47. extern VideoWrapper *myVideo;
  48.  
  49. void Fill_Buffer()
  50. {
  51.   int Buffer_Level;
  52.  
  53.   do{
  54.       Buffer_Level = myVideo->ReadLPES((unsigned char **)&ld->Rdbfr, &myVideo->myPES);
  55.       if(!Buffer_Level) break;
  56.       if(myVideo->myPES.streamID==myVideo->subpic_streamID &&
  57.           myVideo->myPES.subStreamID==myVideo->subpic_substreamID) {
  58.           /* this is a subpic; parse it, and discard it so the video decoder
  59.            * doesn't see it */
  60.  
  61.           subpic_decode(ld->Rdbfr, myVideo->myPES.payloadSize, (int)((double)myVideo->myPES.PTS/27000.0) );
  62.  
  63.           continue;
  64.       }
  65.   }while(myVideo->myPES.streamID!=myVideo->streamID);
  66.  
  67.   VIDEO_BUFFER_SIZE=myVideo->myPES.payloadSize;
  68.   ld->Rdmax=ld->Rdbfr + VIDEO_BUFFER_SIZE;
  69.  
  70.   ld->Rdptr = ld->Rdbfr;
  71.  
  72.   if (System_Stream_Flag)
  73.     ld->Rdmax -= VIDEO_BUFFER_SIZE;
  74.  
  75.   
  76.   /* end of the bitstream file */
  77.   if (Buffer_Level ==0/*< VIDEO_BUFFER_SIZE*/)
  78.   {
  79.     /* just to be safe */
  80.     if (Buffer_Level < 0)
  81.       Buffer_Level = 0;
  82.  
  83.     /* pad until the next to the next 32-bit word boundary */
  84.     while (Buffer_Level & 3)
  85.       ld->Rdbfr[Buffer_Level++] = 0;
  86.  
  87.     /* pad the buffer with sequence end codes */
  88.     while (Buffer_Level < VIDEO_BUFFER_SIZE )
  89.     {
  90.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
  91.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
  92.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
  93.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
  94.     }
  95.   }
  96. }
  97.  
  98. /* initialize buffer, call once before first getbits or showbits */
  99.  
  100.  void Initialize_Buffer()
  101. {
  102.   VIDEO_BUFFER_SIZE=1024;
  103.   ld->Incnt = 0;
  104.   ld->Rdptr = ld->Rdbfr + VIDEO_BUFFER_SIZE;
  105.   //ld->Rdptr = ld->Rdbfr + 2048;
  106.   ld->Rdmax = ld->Rdptr;
  107.  
  108. #ifdef VERIFY
  109.   /*  only the verifier uses this particular bit counter 
  110.    *  Bitcnt keeps track of the current parser position with respect
  111.    *  to the video elementary stream being decoded, regardless 
  112.    *  of whether or not it is wrapped within a systems layer stream 
  113.    */
  114.   ld->Bitcnt = 0;
  115. #endif
  116.  
  117.   ld->Bfr = 0;
  118.   Flush_Buffer(0); /* fills valid data into bfr */
  119.   //Fill_Buffer();
  120.   
  121. }
  122.  
  123.  
  124.  
  125.  
  126. /* MPEG-1 system layer demultiplexer */
  127.  
  128. int Get_Byte()
  129. {
  130.   while(ld->Rdptr >= ld->Rdbfr+VIDEO_BUFFER_SIZE)
  131.   {
  132. //    myVideo->ReadStream((char *)ld->Rdbfr,VIDEO_BUFFER_SIZE);
  133.     ld->Rdptr -= VIDEO_BUFFER_SIZE;
  134.     ld->Rdmax -= VIDEO_BUFFER_SIZE;
  135.   }
  136.   return *ld->Rdptr++;
  137. }
  138.  
  139. /* extract a 16-bit word from the bitstream buffer */
  140. int Get_Word()
  141. {
  142.   int Val;
  143.  
  144.   Val = Get_Byte();
  145.   return (Val<<8) | Get_Byte();
  146. }
  147.  
  148.  
  149. /* return next n bits (right adjusted) without advancing */
  150.  
  151. unsigned int Show_Bits(int N)
  152. {
  153.   return ld->Bfr >> (32-N);
  154. }
  155.  
  156.  
  157. /* return next bit (could be made faster than Get_Bits(1)) */
  158.  
  159. unsigned int Get_Bits1()
  160. {
  161.   return Get_Bits(1);
  162. }
  163.  
  164.  
  165. /* advance by n bits */
  166.  
  167. void Flush_Buffer(int N)
  168. {
  169.   int Incnt;
  170.  
  171.   ld->Bfr <<= N;
  172.  
  173.   Incnt = ld->Incnt -= N;
  174.  
  175.   if (Incnt <= 24)
  176.   {
  177.     if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
  178.     {
  179.       do
  180.       {
  181.         if (ld->Rdptr >= ld->Rdmax)
  182.           Next_Packet();
  183.         ld->Bfr |= Get_Byte() << (24 - Incnt);
  184.         Incnt += 8;
  185.       }
  186.       while (Incnt <= 24);
  187.     }
  188.     else if (ld->Rdptr < ld->Rdbfr+VIDEO_BUFFER_SIZE-4)
  189.     {
  190.       do
  191.       {
  192.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  193.         Incnt += 8;
  194.       }
  195.       while (Incnt <= 24);
  196.     }
  197.     else
  198.     {
  199.       do
  200.       {
  201.         if (ld->Rdptr >= ld->Rdbfr+VIDEO_BUFFER_SIZE)
  202.           Fill_Buffer();
  203.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  204.         Incnt += 8;
  205.       }
  206.       while (Incnt <= 24);
  207.     }
  208.     ld->Incnt = Incnt;
  209.   }
  210.  
  211. #ifdef VERIFY 
  212.   ld->Bitcnt += N;
  213. #endif /* VERIFY */
  214.  
  215. }
  216.  
  217.  
  218. /* return next n bits (right adjusted) */
  219.  
  220. unsigned int  Get_Bits(int N)
  221. {
  222.   unsigned int Val;
  223.  
  224.   Val = Show_Bits(N);
  225.   Flush_Buffer(N);
  226.  
  227.   return Val;
  228. }
  229.  
  230.